package dbPhase.hypeerweb;
/**
 * NeighborRole extends Connection and overrides all the methods to change the connections
 * in reference as a neighbor.
 *
 * @see Connection
 *
 *
 * @author scott44
 *
 */
public class NeighborRole extends Connection {
	public NeighborRole(Node n, Node srcParent) {
		super(n, srcParent);
	}

	@Override
	public void addConnection(Node add) {
		delegate.neighbors.add(add);

		if (delegate.downPointers.contains(sourceParent)) {
			delegate.downPointers.remove(sourceParent);
		}

		if (delegate.upPointers.size() > 0) {
			delegate.nodeState = UpPointingNodeState.getSingleton();
		}
		else if (delegate.downPointers.size() > 0) {
			delegate.nodeState = DownPointingNodeState.getSingleton();
		}
		else {
			delegate.nodeState = StandardNodeState.getSingleton();
		}
	}

	@Override
	public void removeConnection(Node remove) {
		delegate.neighbors.remove(remove);
		remove.neighbors.remove(delegate);
		//add the parent as a downpointer
		if (delegate != sourceParent) {
			delegate.downPointers.add(sourceParent);
			delegate.nodeState = DownPointingNodeState.getSingleton();
			sourceParent.upPointers.add(delegate);
			sourceParent.nodeState = UpPointingNodeState.getSingleton();
			if (delegate.getBiggestNeighbor().getWebIdValue() < delegate.getWebIdValue()) {
				delegate.nodeState = TerminalNodeState.getSingleton();
			}
		}
		else {
			delegate.webId = new WebId(delegate.getWebIdValue(), delegate.getWebId().getHeight() - 1);

		}
		if (delegate.getWebIdValue() == 0 && delegate.neighbors.size() == 0 &&
				delegate.downPointers.size() == 0 && delegate.upPointers.size() == 0) {
			delegate.fold = delegate;
			delegate.inverseSurrogateFold = Node.NULL_NODE;
			delegate.surrogateFold = Node.NULL_NODE;
			delegate.nodeState = HypercubeCapState.getSingleton();
		}
	}

	@Override
	public void replaceConnection(Node oldNode, Node newNode) {
		delegate.neighbors.remove(oldNode);
		oldNode.neighbors.remove(delegate);
		delegate.neighbors.add(newNode);
		newNode.neighbors.add(delegate);
	}
}
